import importlib

import yaml
import numpy as np
from ..satellite_formation import basic_sate_form, simple_sate_form

from .basic_variable import basic_variable
from ..target_list import target_list

class simple_time(basic_variable):
    '''
    简易时间参数，仅考虑卫星编队旋转所需时间。
    '''
    
    def __init__(self, yaml_config: yaml.nodes.Node):
        self.__formation_status = yaml_config.get("InitialFormationStatus", {})    # 队列初始状态

    @staticmethod
    def param_list():
        return ['ra', 'dec', 't_tot']

    def initialize(self, tg_list: target_list):
        #formation_type = 'simple_sate_form'
        #sate_form_clz = getattr(importlib.import_module(f".satellite_formation.{formation_type}", package="miyinff"), formation_type)
        return tg_list, simple_sate_form(self.__formation_status)

    def filter(self, target_list: np.ndarray, sate_form: basic_sate_form):
        # 筛选当前黄道经度满足可视角度要求的目标
        index = np.abs(target_list[:,1] - sate_form.orbit_lon) < sate_form.visual_angle
        target_list = target_list[index]
        
        # 计算完成下一任务需要的总时间（编队转向+t_tot）
        target_finish_time = self.evaluate(target_list, sate_form)

        # 筛选任务完成时仍在可视角度内的目标
        sate_finish_obrit_lon = sate_form.orbit_lon + target_finish_time * sate_form.orbit_rotate_angular_velocity
        index = (target_list[:,1] - sate_finish_obrit_lon) < sate_form.visual_angle
        return target_list[index]

    def evaluate(self, target_list: np.ndarray, sate_form: basic_sate_form) -> np.ndarray:

        delta_lon = np.abs(target_list[:,1]-sate_form.point_lon) % 360
        target_retarget_time = delta_lon / sate_form.angular_velocity

        target_finish_time = target_retarget_time + target_list[:,3]
        #target_finish_time = self.observe() + self.retarget()

        return target_finish_time

    #def retarget(self):
    #    delta_lon = np.abs(self.__target_list[:,1]-self.__sate_form.point_lon) % 360
    #    target_retarget_time = delta_lon / self.__sate_form.angular_velocity
    #    return target_retarget_time

    #def observe(self):
    #    return self.__target_list[:,3]

